package compile_and_download

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"test/unit_function/sshdata"
	"time"
)

type Rk3568Xts struct {
	BuildTarget
}

// Compile 编译任务：上传脚本，执行脚本，下载产物
func (receiver *Rk3568Xts) Compile() error {
	client := sshdata.GetConnection()
	defer client.Close()
	result := sshdata.UploadFile("/mnt/data/l30044646/script_xts.sh", "script/script_xts.sh", client, "0744")
	if !result {
		log.Println("upload file script_xts failed")
	}
	log.Println("uploadFile succeed")
	session, err := client.NewSession()
	if err != nil {
		log.Println("Rk3568Xts newSession failed, error=", err)
		return err
	}
	defer session.Close()
	log.Println("executing script_xts.sh")
	err = session.Run("~/script_xts.sh > ~/script_xts.log")
	if err != nil {
		log.Println("Rk3568Xts run script failed, error=", err)
		return err
	}
	log.Println("executing script_xts.sh succeed!")
	return nil
}

func (receiver *Rk3568Xts) Download() error {
	// 下载文件并解压
	client := sshdata.GetConnection()
	defer client.Close()
	sshdata.DownloadFile("/mnt/data/l30044646/openharmony/out/rk3568/musl/libc-test.tar", "D:/auto_test/xts/suites_tmp/acts/acts/testcases/libc-test.tar", client)
	sshdata.DownloadFile("/mnt/data/l30044646/openharmony/out/rk3568/musl/libc-test-lib.tar", "D:/auto_test/xts/suites_tmp/acts/acts/testcases/libc-test-lib.tar", client)
	//cmd := exec.Command("tar", "-zxvf", "suites_tmp.tar.gz")
	//cmd.Dir = "D:/auto_test/xts"
	//err := cmd.Run()
	//if err != nil {
	//	log.Println("error occurred when executing tar -zxvf suites_tmp.tar.gz ", err)
	//	return err
	//}
	//log.Println("executing tar -zxvf suites_tmp.tar.gz succeed")
	//log.Println("download suites_tmp.tar.gz succeed!")
	log.Println("download libc-test succeed!")
	return nil
}

func (receiver *Rk3568Xts) Execute() error {
	// 1.执行测试用例
	log.Println("executing", "python", "-m", "xdevice", "run -l ActsToolChainTest")
	cmd := exec.Command("python", "-m", "xdevice", "run -l ActsToolChainTest")
	cmd.Dir = "D:/auto_test/xts/suites_tmp/acts/acts"
	err := cmd.Run()
	if err != nil {
		log.Println("error occurred when executing xdevice, error msg = ", err)
		return err
	}
	log.Println("finding latest...")

	cmd = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", "xtsLatest")
	cmd.Dir = "D:/auto_test/xts/suites_tmp/acts/reports"
	err = cmd.Run()
	// 2.打包结果
	dir, err := os.ReadDir("D:/auto_test/xts/suites_tmp/acts/acts/reports")
	if err != nil {
		log.Println("readDir failed, error msg:", err)
		return err
	}
	var maxTime time.Time
	var fileName string
	maxTime = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
	for _, entry := range dir {
		info, err := entry.Info()
		if err != nil {
			log.Println("error when get entry info ", err)
		}
		cntTime := info.ModTime()
		if info.IsDir() && maxTime.Before(cntTime) {
			maxTime = cntTime
			fileName = info.Name()
		}
	}
	log.Println("latest is", fileName)

	cmd = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", "xtsLatest")
	cmd.Dir = "D:/auto_test/result"
	err = cmd.Run()
	cmd = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", "xtsLatest.tar.gz")
	cmd.Dir = "D:/auto_test/result"
	err = cmd.Run()

	//if err != nil {
	//	log.Println("error executing delete xtsLatest err msg:", err)
	//	return err
	//}
	cmd = exec.Command("cmd", "/C", "ren", fileName, "xtsLatest")
	cmd.Dir = "D:/auto_test/xts/suites_tmp/acts/acts/reports"
	err = cmd.Run()
	for err != nil {
		log.Println("error executing rename from D:/auto_test/xts/suites_tmp/acts/acts/reports"+fileName, "to D:/auto_test/xts/suites_tmp/acts/acts/reports/xtsLatest err msg:", err)
		cmd = exec.Command("cmd", "/C", "ren", fileName, "xtsLatest")
		cmd.Dir = "D:/auto_test/xts/suites_tmp/acts/acts/reports"
		err = cmd.Run()
		// 拼接 xtsLatest 文件夹路径
		xtsLatestDir := fmt.Sprintf("%s/xtsLatest", cmd.Dir)

		// 检查 xtsLatest 文件夹是否存在
		_, err := os.Stat(xtsLatestDir)
		if err == nil {
			break
		}
		time.Sleep(time.Second * 3)
	}
	fileName = "xtsLatest"
	cmd = exec.Command("tar", "-zcvf", "xtsLatest.tar.gz", fileName)
	cmd.Dir = "D:/auto_test/xts/suites_tmp/acts/acts/reports"
	err = cmd.Run()
	if err != nil {
		log.Println("error occurred when executing tar -zcvf, error msg = ", err)
		return err
	}
	// 3.移动文件
	log.Println("moving result...", "D:/auto_test/xts/suites_tmp/acts/acts/reports/xts_latest.tar.gz", "-->", "D:/auto_test/result/xts_latest.tar.gz")
	err = os.Rename("D:/auto_test/xts/suites_tmp/acts/acts/reports/xtsLatest.tar.gz", "D:/auto_test/result/xtsLatest.tar.gz")
	if err != nil {
		log.Println("error occurred when moving xts_latest.tar.gz, error msg = ", err)
		return err
	}
	// 4.解压新文件
	log.Println("tar -zxvf", "D:/auto_test/result/xts_latest.tar.gz")
	cmd = exec.Command("tar", "-zxvf", "xtsLatest.tar.gz")
	cmd.Dir = "D:/auto_test/result"
	err = cmd.Run()
	if err != nil {
		log.Println("error occurred when executing tar -zxvf xts_latest.tar.gz, error msg = ", err)
		return err
	}
	log.Println("rk3568_xts execute succeed!")
	return nil
}
